package com.ruoyi.web.task;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.supervise.domain.ProjectArchives;
import com.ruoyi.supervise.domain.TbFocusProject;
import com.ruoyi.supervise.service.IProjectArchivesService;
import com.ruoyi.supervise.service.ITaskService;
import com.ruoyi.supervise.service.ITbFocusProjectService;
import com.ruoyi.web.util.ListUtil;

/**
 * @ClassName: SynchronizationDataTask
 * @Description: 同步项目表数据到关注项目表定时任务
 * @Author wtj
 * @DateTime 2020年5月14日 上午11:44:05
 */
@Component("synDataTask")
public class SynchronizationDataTask {
	
	@Autowired
    private IProjectArchivesService projectArchivesService;
	
	@Autowired
    private ITbFocusProjectService tbFocusProjectService;
	
	@Autowired
    private ITaskService taskService;
	
	/**
	 * @Title: focusProjectTask
	 * @Description: 由于出现会修改Base_ProjectInfo表PubDate字段的问题，所以修改该定时任务，不用PubDate字段排序
	 * @Author wtj
	 * @DateTime 2020年5月26日 上午9:56:11
	 */
    public void focusProjectTask() {
    	int projectTotal = projectArchivesService.countBaseProjectInfo();
    	int tbFocusProject = taskService.countTbFocusProject();
    	//计算出项目表和关注项目表数据量的差值
    	int subNum = projectTotal-tbFocusProject;
    	int result = 0;
    	System.err.println("执行同步项目表数据到关注项目表定时任务时间: " + LocalDateTime.now());
        System.out.println("两个表相差："+subNum);
    	if(subNum>0) {
    		List<ProjectArchives> paallList = projectArchivesService.selectBaseProjectInfoAll();
    		List<TbFocusProject> fpallList = tbFocusProjectService.selectTbFocusProjectAll();
    		List<ProjectArchives> projectArchiveslist = new ArrayList<ProjectArchives>();
    		for(ProjectArchives paa:paallList) {
    			if(!StringUtils.isEmpty(fpallList)) {
    				boolean isEq = false;
    				for(TbFocusProject fpa:fpallList) {
    					isEq = false;
        				if(paa.getGuid().equals(fpa.getGuid())) {
        					isEq = true;
        					break;
        				}
        			}
        			if(!isEq) {
        				paa.setInsertTime(DateUtils.dateTimeNow("yyyy-MM-dd HH:mm:ss.SSS"));
        				projectArchiveslist.add(paa);
        			}
    			}else {
    				paa.setInsertTime(DateUtils.dateTimeNow("yyyy-MM-dd HH:mm:ss.SSS"));
    				projectArchiveslist.add(paa);
    			}
    		}
    		if(!StringUtils.isEmpty(projectArchiveslist)) {
    			//传入的参数最多是2100个数量，大于的话要对集合进行分割
        		if((projectArchiveslist.size()*11)>2100) {
        			List<List<ProjectArchives>> list=ListUtil.splitList(projectArchiveslist,180);
        			for(int i=0;i<list.size();i++) {
        				result = taskService.synchronizationData(list.get(i));
        			}
        		}else {
        			result = taskService.synchronizationData(projectArchiveslist);
        		}
    		}
    		if(result>0) {
    			System.out.println("更新成功");
    		}else {
    			System.err.println("更新失败");
    		}
    	}
    	System.out.println("===========================结束=======================");
    }
}
